/*   1:    */package com.linkage.bss.commons.util;
/*   2:    */
/*   3:    */import java.math.BigDecimal;
/*   4:    */import java.math.BigInteger;
/*   5:    */import java.util.ArrayList;
/*   6:    */import java.util.Collection;
/*   7:    */import java.util.Date;
/*   8:    */import java.util.HashMap;
/*   9:    */import java.util.Iterator;
/*  10:    */import java.util.List;
/*  11:    */import java.util.Map;
/*  12:    */import java.util.concurrent.atomic.AtomicInteger;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*  13:    */import net.sf.ezmorph.MorpherRegistry;
/*  14:    */import net.sf.ezmorph.object.DateMorpher;
/*  15:    */import net.sf.json.JSONArray;
/*  16:    */import net.sf.json.JSONNull;
/*  17:    */import net.sf.json.JSONObject;
/*  18:    */import net.sf.json.JsonConfig;
/*  19:    */import net.sf.json.processors.DefaultValueProcessor;
/*  20:    */import net.sf.json.util.JSONUtils;
/*  21:    */
/*  29:    */public class JsonUtil
/*  30:    */{
/*  31: 31 */  private static Logger log = LoggerFactory.getLogger(JsonUtil.class);
/*  32:    */  
/*  42: 42 */  public static final JsonConfig NUMBER_NULL_JSONCONF = createNumberNullValueJsonConfig();
/*  43:    */  
/*  44: 44 */  public static final JsonConfig DEFAULT_AND_DATTE_JSONCONF = createDefaultAndDateJsonConfig();
/*  45:    */  
/*  53:    */  public static Object getObject(String jsonString, Class clazz)
/*  54:    */  {
/*  55: 55 */    JSONObject jsonObject = null;
/*  56:    */    try {
/*  57: 57 */      setDataFormat2JAVA();
/*  58: 58 */      jsonObject = JSONObject.fromObject(jsonString);
/*  59:    */    } catch (Exception e) {
/*  60: 60 */      log.error("转换object异常:", e);
/*  61:    */    }
/*  62: 62 */    return JSONObject.toBean(jsonObject, clazz);
/*  63:    */  }
/*  64:    */  
/*  76:    */  public static Object getObject(String jsonString, Class clazz, Map map)
/*  77:    */  {
/*  78: 78 */    JSONObject jsonObject = null;
/*  79:    */    try {
/*  80: 80 */      setDataFormat2JAVA();
/*  81: 81 */      jsonObject = JSONObject.fromObject(jsonString);
/*  82:    */    } catch (Exception e) {
/*  83: 83 */      log.error("json string convert to obj error:", e);
/*  84: 84 */      return null;
/*  85:    */    }
/*  86: 86 */    return JSONObject.toBean(jsonObject, clazz, map);
/*  87:    */  }
/*  88:    */  
/*  96:    */  public static Object[] getObjectArray(String jsonString, Class clazz)
/*  97:    */  {
/*  98: 98 */    setDataFormat2JAVA();
/*  99: 99 */    JSONArray array = JSONArray.fromObject(jsonString);
/* 100:100 */    Object[] obj = new Object[array.size()];
/* 101:101 */    for (int i = 0; i < array.size(); i++) {
/* 102:102 */      JSONObject jsonObject = array.getJSONObject(i);
/* 103:103 */      obj[i] = JSONObject.toBean(jsonObject, clazz);
/* 104:    */    }
/* 105:105 */    return obj;
/* 106:    */  }
/* 107:    */  
/* 116:    */  public static Object[] getObjectArray(String jsonString, Class clazz, Map map)
/* 117:    */  {
/* 118:118 */    setDataFormat2JAVA();
/* 119:119 */    JSONArray array = JSONArray.fromObject(jsonString);
/* 120:120 */    Object[] obj = new Object[array.size()];
/* 121:121 */    for (int i = 0; i < array.size(); i++) {
/* 122:122 */      JSONObject jsonObject = array.getJSONObject(i);
/* 123:123 */      obj[i] = JSONObject.toBean(jsonObject, clazz, map);
/* 124:    */    }
/* 125:125 */    return obj;
/* 126:    */  }
/* 127:    */  
/* 134:    */  public static List getObjectList(String jsonString, Class clazz)
/* 135:    */  {
/* 136:136 */    setDataFormat2JAVA();
/* 137:137 */    JSONArray array = JSONArray.fromObject(jsonString);
/* 138:138 */    List list = new ArrayList();
/* 139:139 */    for (Iterator iter = array.iterator(); iter.hasNext();) {
/* 140:140 */      JSONObject jsonObject = (JSONObject)iter.next();
/* 141:141 */      list.add(JSONObject.toBean(jsonObject, clazz));
/* 142:    */    }
/* 143:143 */    return list;
/* 144:    */  }
/* 145:    */  
/* 154:    */  public static List getObjectList(String jsonString, Class clazz, Map map)
/* 155:    */  {
/* 156:156 */    setDataFormat2JAVA();
/* 157:157 */    JSONArray array = JSONArray.fromObject(jsonString);
/* 158:158 */    List list = new ArrayList();
/* 159:159 */    for (Iterator iter = array.iterator(); iter.hasNext();) {
/* 160:160 */      JSONObject jsonObject = (JSONObject)iter.next();
/* 161:161 */      list.add(JSONObject.toBean(jsonObject, clazz, map));
/* 162:    */    }
/* 163:163 */    return list;
/* 164:    */  }
/* 165:    */  
/* 171:    */  public static Map getMap(String jsonString)
/* 172:    */  {
/* 173:173 */    setDataFormat2JAVA();
/* 174:174 */    JSONObject jsonObject = JSONObject.fromObject(jsonString);
/* 175:175 */    Map map = new HashMap();
/* 176:176 */    for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
/* 177:177 */      String key = (String)iter.next();
/* 178:178 */      map.put(key, jsonObject.get(key));
/* 179:    */    }
/* 180:180 */    return map;
/* 181:    */  }
/* 182:    */  
/* 188:    */  public static Object[] getObjectArray(String jsonString)
/* 189:    */  {
/* 190:190 */    JSONArray jsonArray = JSONArray.fromObject(jsonString);
/* 191:191 */    return jsonArray.toArray();
/* 192:    */  }
/* 193:    */  
/* 194:    */  private static void setDataFormat2JAVA()
/* 195:    */  {
/* 196:196 */    JSONUtils.getMorpherRegistry().registerMorpher(
/* 197:197 */      new DateMorpher(new String[] { "yyyy-MM-dd", 
/* 198:198 */      "yyyy-MM-dd HH:mm:ss" }));
/* 199:    */  }
/* 200:    */  
/* 206:    */  public static String getJsonString(Object obj)
/* 207:    */  {
/* 208:208 */    if (obj == null)
/* 209:209 */      return "{}";
/* 210:210 */    return getJsonString(obj, DEFAULT_AND_DATTE_JSONCONF);
/* 211:    */  }
/* 212:    */  
/* 217:    */  public static String getJsonString(Object obj, JsonConfig cfg)
/* 218:    */  {
/* 219:219 */    if (obj != null)
/* 220:    */    {
/* 221:221 */      if (isArray(obj)) {
/* 222:222 */        JSONArray jsonArray = JSONArray.fromObject(obj, cfg);
/* 223:223 */        return jsonArray.toString();
/* 224:    */      }
/* 225:    */      
/* 226:226 */      JSONObject jsonObject = JSONObject.fromObject(obj, cfg);
/* 227:227 */      return jsonObject.toString();
/* 228:    */    }
/* 229:    */    
/* 230:230 */    return "{}";
/* 231:    */  }
/* 232:    */  
/* 236:    */  private static boolean isArray(Object obj)
/* 237:    */  {
/* 238:238 */    return ((obj instanceof Collection)) || (obj.getClass().isArray());
/* 239:    */  }
/* 240:    */  
/* 243:    */  public static JsonConfig createNumberNullValueJsonConfig()
/* 244:    */  {
/* 245:245 */    JsonConfig conf = new JsonConfig();
/* 246:246 */    registerDefaultNullValueProcessor(conf);
/* 247:247 */    return conf;
/* 248:    */  }
/* 249:    */  
/* 253:    */  public static JsonConfig createDefaultAndDateJsonConfig()
/* 254:    */  {
/* 255:255 */    JsonConfig conf = new JsonConfig();
/* 256:256 */    registerDefaultNullValueProcessor(conf);
/* 257:257 */    registerDateValueProcessor(conf);
/* 258:258 */    return conf;
/* 259:    */  }
/* 260:    */  
/* 263:    */  public static void registerDateValueProcessor(JsonConfig conf)
/* 264:    */  {
/* 265:265 */    conf.registerJsonValueProcessor(Date.class, 
/* 266:266 */      new JsonDateValueProcessor());
/* 267:    */  }
/* 268:    */  
/* 271:    */  public static void registerDefaultNullValueProcessor(JsonConfig conf)
/* 272:    */  {
/* 273:273 */    conf.registerDefaultValueProcessor(Number.class, new DefaultNullValueProcessor());
/* 274:274 */    conf.registerDefaultValueProcessor(AtomicInteger.class, new DefaultNullValueProcessor());
/* 275:275 */    conf.registerDefaultValueProcessor(BigDecimal.class, new DefaultNullValueProcessor());
/* 276:276 */    conf.registerDefaultValueProcessor(BigInteger.class, new DefaultNullValueProcessor());
/* 277:277 */    conf.registerDefaultValueProcessor(Byte.class, new DefaultNullValueProcessor());
/* 278:278 */    conf.registerDefaultValueProcessor(Double.class, new DefaultNullValueProcessor());
/* 279:279 */    conf.registerDefaultValueProcessor(Float.class, new DefaultNullValueProcessor());
/* 280:280 */    conf.registerDefaultValueProcessor(Integer.class, new DefaultNullValueProcessor());
/* 281:281 */    conf.registerDefaultValueProcessor(Long.class, new DefaultNullValueProcessor());
/* 282:282 */    conf.registerDefaultValueProcessor(Short.class, new DefaultNullValueProcessor());
/* 283:    */  }
/* 284:    */  
/* 286:    */  public static class DefaultNullValueProcessor
/* 287:    */    implements DefaultValueProcessor
/* 288:    */  {
/* 289:    */    public Object getDefaultValue(Class type)
/* 290:    */    {
/* 291:291 */      return JSONNull.getInstance();
/* 292:    */    }
/* 293:    */  }
/* 294:    */}


/* Location:           E:\workspace\sungl111\workspace_promotion\BizHallConfig\WebRoot\WEB-INF\lib\bss.commons-0.1.jar
 * Qualified Name:     com.linkage.bss.commons.util.JsonUtil
 * JD-Core Version:    0.7.0-SNAPSHOT-20130630
 */